package com.jd.xsl.writer;


public class StyleAttrParser {

	public static StyleAttribute parse(String style) {
		if (style == null) {
			return null;
		}

		StyleAttribute result = new StyleAttribute();

		String[] attrs = style.split(";");
		for (String attr : attrs) {
			if ("".equals(attr.trim()) || attr.split(":").length == 0) {
				continue;
			}

			String key = attr.split(":")[0].trim();
			String value = attr.split(":")[1].trim();

			if (StyleAttribute.ATTR_WIDTH.equalsIgnoreCase(key)) {
				int width = Integer.parseInt(value.substring(0, value.length() - 2).trim());
				result.setWidth(width);
				continue;
			}

			if (StyleAttribute.ATTR_HEIGHT.equalsIgnoreCase(key)) {
				int height = Integer.parseInt(value.substring(0, value.length() - 2).trim());
				result.setHeight(height);
				continue;
			}

			if (StyleAttribute.ATTR_BACKGROUND_COLOR.equalsIgnoreCase(key)) {
				result.setBackgroundColor(value);
				continue;
			}

			if (StyleAttribute.ATTR_COLOR.equalsIgnoreCase(key)) {
				result.setFontColor(value);
				continue;
			}

			if (StyleAttribute.ATTR_FONT_SIZE.equalsIgnoreCase(key)) {
				int fontSize = Integer.parseInt(value.substring(0, value.length() - 2).trim());
				result.setFontSize(fontSize);
				continue;
			}

			if (StyleAttribute.ATTR_FONT_WEIGHT.equalsIgnoreCase(key)) {
				result.setFontWeight(value);
				continue;
			}

			if (StyleAttribute.ATTR_TEXT_ALIGN.equalsIgnoreCase(key)) {
				result.setAlign(value);
				continue;
			}

			if (StyleAttribute.ATTR_VERTICAL_ALIGN.equalsIgnoreCase(key)) {
				result.setValign(value);
				continue;
			}

			if (StyleAttribute.ATTR_DISPLAY.equalsIgnoreCase(key)) {
				result.setDisplay(value);
				continue;
			}
		}

		return result;
	}
}